package Exercise;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class TCPServerExercise01 {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端9999等待连接...");
        Socket socket = serverSocket.accept();

        //接收
        InputStream inputStream = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String s = br.readLine();
        String answer = " ";
        if(s.equals("name")){
        answer = "我是WYX";
        }else if(s.equals("hobby")){
            answer = "敲代码";
        }else {
            answer = "你在狗叫什么";
        }

        //回复
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
        bw.write(answer);
        bw.newLine();
        bw.flush();
        socket.shutdownOutput();


        //关闭
        bw.close();
        br.close();
        socket.close();
        serverSocket.close();


    }
}
